//
//  AppDelegate.m
//  ZzzzyManProject
//
//  Created by mac on 16/2/24.
//  Copyright © 2016年 ZzzzyMan@. All rights reserved.
//

#import "AppDelegate.h"
#import "MainTabbarController.h"
#import "MMDrawerController.h"
#import "InfoViewController.h"
#import "HomeViewController.h"
#import <AVOSCloud/AVOSCloud.h>
//Mob分享
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
//腾讯开放平台 -QQ -QQ空间
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
//微信
#import "WXApi.h"
//新浪微博
#import "WeiboSDK.h"

@interface AppDelegate ()

@property (nonatomic,strong) MMDrawerController * drawerVC;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //设置友盟AppKey
//    [UMSocialData setAppKey:@"56dd1960e0f55a79c6000ac3"];
    
    //初始化LeanCloudSDK
    [AVOSCloud setApplicationId:@"jp7y4rwfnVwil0eCaKQ0QBfB-gzGzoHsz"
                      clientKey:@"XCvknI1ROneILxFPklTiXWPN"];
    
    // 让启动图片停留更长时间
//    [NSThread sleepForTimeInterval:2.0];
    
    //注册本地通知
    [self localNotifcation];

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    
    self.window.rootViewController = [self DrawerViewController];
    
    [self.window makeKeyAndVisible];
    
    //Mob分享设置
    [self setupMob];
    
    
    return YES;
}

#pragma mark - Mob分享设置
- (void)setupMob {
    [ShareSDK registerApp:@"iosv1101"
     
          activePlatforms:@[
                            @(SSDKPlatformTypeSinaWeibo),
                            @(SSDKPlatformTypeMail),
                            @(SSDKPlatformTypeSMS),
                            @(SSDKPlatformTypeWechat),
                            @(SSDKPlatformTypeQQ)]
        
                 onImport:^(SSDKPlatformType platformType)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeWechat:
                 [ShareSDKConnector connectWeChat:[WXApi class]];
                 break;
             case SSDKPlatformTypeQQ:
                 [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                 break;
             case SSDKPlatformTypeSinaWeibo:
                 [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                 break;
             default:
                 break;
         }
     }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
     {
         
         switch (platformType)
         {
             case SSDKPlatformTypeSinaWeibo:
                 //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                 [appInfo SSDKSetupSinaWeiboByAppKey:@"1269974159"
                                           appSecret:@"3d050966a92346021d586e5d5c38e7a0"
                                         redirectUri:@"http://www.apple.com"
                                            authType:SSDKAuthTypeBoth];
                 break;
             case SSDKPlatformTypeWechat:
                 [appInfo SSDKSetupWeChatByAppId:@"wx6443695ac17c6be3"
                                       appSecret:@"16a77910389efabf112775ff8d722b38"];
                 break;
             case SSDKPlatformTypeQQ:
                 [appInfo SSDKSetupQQByAppId:@"100371282"
                                      appKey:@"aed9b0303e3ed1e27bae87c33761161d"
                                    authType:SSDKAuthTypeBoth];
                 break;
             default:
                 break;
         }
     }];

}


#pragma mark - 抽屉效果
- (UIViewController *)DrawerViewController {
    
    //抽屉效果
    MainTabbarController * tabBarVC = [[MainTabbarController alloc] init];
    
    //初始化子视图
    InfoViewController * infoVC = [[InfoViewController alloc] init];

    //初始化抽屉视图控制器
    self.drawerVC = [[MMDrawerController alloc] initWithCenterViewController:tabBarVC leftDrawerViewController:infoVC];
    
    //设置抽屉抽出的宽度
    self.drawerVC.maximumLeftDrawerWidth = 300;
    
    //添加滑动手势
    [self.drawerVC setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeBezelPanningCenterView];
    [self.drawerVC setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    //设置阴影
    [self.drawerVC setShowsShadow:false];
    
    return self.drawerVC;
    
}

#pragma mark - 本地通知
- (void)localNotifcation {
    // 设置本地通知
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        // 设置触发通知的时间
        NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
        
        
        notification.fireDate = fireDate;
        // 时区
        notification.timeZone = [NSTimeZone defaultTimeZone];
        // 设置重复的间隔
        notification.repeatInterval = NSCalendarUnitDay;
        
        NSLog(@"fireDate=%@",notification.fireDate);
        
        // 通知内容
        notification.alertBody =  @"通知测试";
        notification.alertTitle = @"ZzzzyMan提醒您通知测试成功";
        notification.applicationIconBadgeNumber = 1;
        // 通知被触发时播放的声音
        notification.soundName = UILocalNotificationDefaultSoundName;
        // 通知参数
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"测试" forKey:@"key"];
        notification.userInfo = userDict;
        
        // ios8后，需要添加这个注册，才能得到授权
        if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
            UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                                                                     categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
            // 通知重复提示的单位，可以是天、周、月
            notification.repeatInterval = NSCalendarUnitDay;
        } else {
            // 通知重复提示的单位，可以是天、周、月
            notification.repeatInterval = NSCalendarUnitMonth;
        }
        
        // 执行通知注册
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }


#pragma mark - sinaWeibo SSO
//- (BOOL)application:(UIApplication *)application
//      handleOpenURL:(NSURL *)url
//{
//    return [ShareSDK hasAuthorized:ssd];
//}
//
//- (BOOL)application:(UIApplication *)application
//            openURL:(NSURL *)url
//  sourceApplication:(NSString *)sourceApplication
//         annotation:(id)annotation
//{
//    return [ShareSDK handleOpenURL:url
//                 sourceApplication:sourceApplication
//                        annotation:annotation
//                        wxDelegate:self];
//}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    //取消徽章
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark - 接受到通知的时候
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    // 获取通知所带的数据
    NSString *notMess = [notification.userInfo objectForKey:@"key"];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"本地通知(前台)"
                                                    message:notMess
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    
    //Appicon上的指示数
    [UIApplication sharedApplication].applicationIconBadgeNumber = 1;

}
@end
